import requests

import matplotlib.pyplot as plt
try:
    abc = "https://hq.techgp.cn/rjhy-gmg-quote/api/1/stock/quotes/shszstocksort?en_hq_type_code=XSHG.ESA%2CXSHE.ESA%2CXSHG.KSH&sort_field_name=px_change_rate&sort_type=1&pageNo=1&pageSize=20"
    sum = requests.get(abc).json()['data']['Total']
    url = "https://hq.techgp.cn/rjhy-gmg-quote/api/1/stock/quotes/shszstocksort?en_hq_type_code=XSHG.ESA%2CXSHE.ESA%2CXSHG.KSH&sort_field_name=px_change_rate&sort_type=1&pageNo=1&pageSize=" + str(
        sum)
    res = requests.get(url).json()['data']['Stocks']

    # 提取股票名称和涨跌幅
    stock_names = [stock['Prod_name'] for stock in res]
    px_change_rates = [stock['Px_change_rate'] for stock in res]

    # 创建柱状图
    plt.figure(figsize=(10, 6))
    plt.bar(stock_names, px_change_rates, color='skyblue')
    plt.xlabel('股票名称')
    plt.ylabel('涨跌幅 (%)')
    plt.title('股票涨跌幅柱状图')
    plt.xticks(rotation=45)  # 旋转股票名称以便更好地显示
    plt.tight_layout()  # 自动调整子图参数，使之填充整个图像区域

    # 显示图表
    plt.show()

except Exception as e:
    print(e)
finally:
    print("Program end!")






